package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper ;

    public void delete(Long id) {
        roleMapper.delete(id);
    }

    public void insert(Role role, Long[] ids) {
        roleMapper.insert(role);
        if(ids!=null){
            for (Long permissionId : ids) {
                roleMapper.insertRelation(role.getId(),permissionId );
            }
            }
        }


    public Role selectOne(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRelation(role.getId());
        if(ids!=null && ids.length>0){
            for(Long permissionId : ids){
                roleMapper.insertRelation(role.getId(),permissionId );
            }
        }
    }

    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize() );
        List<Role> list = roleMapper.queryForList(qo);
        return new PageInfo<Role>(list);
    }
}
